package cn.ffcs.uec.organization.component;

import lombok.Getter;
import lombok.Setter;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zul.Div;

import cn.ffcs.uec.common.manager.CustomFilesManager;
import cn.ffcs.uec.common.model.CustomFiles;
import cn.ffcs.uec.organization.component.bean.UecOrganizationInfoExtBean;
import cn.ffcs.uec.organization.constant.UecOrganizationConstant;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.PubUtil;

/**
 * @Title:用于点击内部树后显示节点的信息
 * @Description:将组织（单位）与部门信息放在一起，根据OrgType来分别展示
 * @Copyright: FFCS（C） 2017
 * @Company: FFCS
 * @author Wang Yiren
 * @Version 0.0.1 date:2017年12月13日
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class UecOrganizationInfoExt extends Div implements IdSpace {

	private static final long serialVersionUID = -1029635625834411797L;
	
	private Logger logger = Logger.getLogger(this.getClass());

	private UecOrganizationInfoExtBean bean = new UecOrganizationInfoExtBean();
	
	private CustomFilesManager customFilesManager = (CustomFilesManager) ApplicationContextUtil
			.getBean("customFilesManager");
	
	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");

	private UecOrgOrgs uecOrgOrgs;

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecOrganizationInfoExt() {
		Executions.createComponents(
				UecOrganizationConstant.UEC_ORGANIZATION_INFO_EXT_ZUL, this,
				null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(
				UecOrganizationConstant.ON_SELECT_UECORGORGS_TREE_REQUEST,
				this, "onSelectUecOrgOrgsTreeResponse");
		this.addForward(
				UecOrganizationConstant.ON_CLEAN_SELECT_UECORGORGS_TREE_REQUEST,
				this, "onCleanSelectUecOrgOrgsTreeResponse");
	}
	/**
	 * 初始化
	 */
	public void onCreate() {

	}
	/**
	 * 事件监听
	 * @param event
	 */
	public void onSelectUecOrgOrgsTreeResponse(ForwardEvent event) {
		uecOrgOrgs = (UecOrgOrgs) event.getOrigin().getData();
		if (null != uecOrgOrgs) {
			logger.info("单位："+uecOrgOrgs.getName());
			PubUtil.fillBeanFromPo(uecOrgOrgs, bean);
			this.groupDataTranslationDisplay();
		}
	}
	
	public void onCleanSelectUecOrgOrgsTreeResponse(ForwardEvent event){
		uecOrgOrgs = null;
	}
	
	/**
	 * 集团字段翻译
	 */
	public void groupDataTranslationDisplay(){
		CustomFiles customFiles = new CustomFiles();
		if(null != uecOrgOrgs){
			//上级组织
			if(null != uecOrgOrgs.getPkFatherorg()){
				UecOrgOrgs objUecOrg = UecOrgOrgs.newInstance();
				objUecOrg.setPkOrg(uecOrgOrgs.getPkFatherorg());
				UecOrgOrgs findUecOrgOrgsByUecOrgOrgs = uecOrgOrgsManager.findUecOrgOrgsByUecOrgOrgs(objUecOrg);
				if(null != findUecOrgOrgsByUecOrgOrgs){
					bean.getPkFatherorg().setValue(findUecOrgOrgsByUecOrgOrgs.getName());
				}else if("0001A11000000000DE9G".equals(uecOrgOrgs.getPkFatherorg())){
					bean.getPkFatherorg().setValue("中国电信股份有限公司");
				}
			}
			//行政划分
			if(null != uecOrgOrgs.getGlbdef12()){
				customFiles.setPkDefdoc(uecOrgOrgs.getGlbdef12());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef12().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			//机构等级
			if(null != uecOrgOrgs.getGlbdef13()){
				customFiles.setPkDefdoc(uecOrgOrgs.getGlbdef13());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef13().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			//公司类型
			if(null != uecOrgOrgs.getGlbdef2()){
				customFiles.setPkDefdoc(uecOrgOrgs.getGlbdef2());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef2().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			//公司性质
			if(null != uecOrgOrgs.getGlbdef3()){
				customFiles.setPkDefdoc(uecOrgOrgs.getGlbdef3());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef3().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			//服务区域
			if(null != uecOrgOrgs.getGlbdef7()){
				customFiles.setPkDefdoc(uecOrgOrgs.getGlbdef7());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef7().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			//单位属性
			if(null != uecOrgOrgs.getGlbdef8()){
				customFiles.setPkDefdoc(uecOrgOrgs.getGlbdef8());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef8().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			//单位层级
			if(null != uecOrgOrgs.getGlbdef9()){
				customFiles.setPkDefdoc(uecOrgOrgs.getGlbdef9());
				CustomFiles findCustomFilesByCustomFiles = customFilesManager.findCustomFilesByCustomFiles(customFiles);
				if(null != findCustomFilesByCustomFiles){
					bean.getGlbdef9().setValue(findCustomFilesByCustomFiles.getName());
				}
			}
			//是否业务单元数据
			if(null != uecOrgOrgs.getIsbusinessunit()){
				String isbusinessunit = uecOrgOrgs.getIsbusinessunit();
				if("Y".equals(isbusinessunit)){
					bean.getIsbusinessunit().setValue("是");
				}else if("N".equals(isbusinessunit)){
					bean.getIsbusinessunit().setValue("否");
				}
			}
			//是否最新版本
			if(null != uecOrgOrgs.getIslastversion()){
				String islastversion = uecOrgOrgs.getIslastversion();
				if("Y".equals(islastversion)){
					bean.getIslastversion().setValue("是");
				}else if("N".equals(islastversion)){
					bean.getIslastversion().setValue("否");
				}
			}
		}
	}

}
